﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using StudentEnrollment.Api.DTOs.Enrollment;
using StudentEnrollment.Api.DTOs.Student;
using StudentEnrollment.Data;
using StudentEnrollment.Data.Contracts;

namespace StudentEnrollment.Api.Endpoints
{
    public static class StudentEndpoints
    {

        public static void MapStudentEndpoints(this IEndpointRouteBuilder router)
        {
            router.MapGet("/api/Student", async (IStudentRepository db, IMapper mapper) =>
            {
                var students = await db.GetAllAsync();
                var data = mapper.Map<List<StudentDto>>(students);
                return data;
            })
                .WithTags(nameof(Student))
                .WithName("GetAllStudents")
                .Produces<List<StudentDto>>(StatusCodes.Status200OK);




            router.MapGet("/api/Student/{id}", async (int id, IStudentRepository db, IMapper mapper) =>
            {
                return await db.GetAsync(id) is Student model ? Results.Ok(mapper.Map<StudentDto>(model))
                    : Results.NotFound();
            })
                .WithTags(nameof(Student))
                .WithName("GetStudentById")
                .Produces<StudentDto>(StatusCodes.Status200OK)
                .Produces(StatusCodes.Status404NotFound);



            router.MapGet("/api/Student/GetDetails/{id}", async (int id, IStudentRepository db, IMapper mapper) =>
            {
                return await db.GetStudentDetails(id) is Student model ? Results.Ok(mapper.Map<StudentDetailsDto>(model))
                    : Results.NotFound();
            })
                .WithTags(nameof(Student))
                .WithName("GetStudentDetailsById")
                .Produces<StudentDetailsDto>(StatusCodes.Status200OK)
                .Produces(StatusCodes.Status404NotFound);



            router.MapPut("/api/Student/{id}", async (int Id, StudentDto studentDto, IStudentRepository db, IMapper mapper) =>
            {
                var foundModel = await db.GetAsync(Id);
                if (foundModel is null)
                {
                    return Results.NotFound();
                }
                mapper.Map(studentDto, foundModel);
                // update model properties here 
                await db.UpdateAsync(foundModel);
                return Results.NoContent();
            })
                .WithTags(nameof(Student))
                .WithName("UpdateStudent")
                .Produces(StatusCodes.Status404NotFound)
                .Produces(StatusCodes.Status204NoContent);




            router.MapPost("/api/Student", async (CreateStudentDto createStudentDto, IStudentRepository db, IMapper mapper) =>
            {
                var student = mapper.Map<Student>(createStudentDto);

                await db.AddAsync(student);
                return Results.Created($"/Students/{student.Id}", student);
            })
                .WithTags(nameof(Student))
                .WithName("CreateStudent")
                .Produces<Student>(StatusCodes.Status201Created);





            router.MapDelete("/api/Student/{id}", async (int Id, IStudentRepository db) =>
            {
                return await db.DeleteAsync(Id) ? Results.NoContent() : Results.NotFound();
            })
                .WithTags(nameof(Student))
                .WithName("DeleteStudent")
                .Produces<Student>(StatusCodes.Status200OK)
                .Produces(StatusCodes.Status404NotFound);

        }
    }
}
